var bShowAlert = false;

function erraRenderLocations() {
    for (var agendaPosition in window.agendaIdArray) {
        agendaId = window.agendaIdArray[agendaPosition];
        if (window.agendaIdLocationName.hasOwnProperty(agendaId)) {
            var locationName = window.agendaIdLocationName[agendaId];
            $('td.wc-user-' + agendaPosition).each(function() {
                $(this).css('background-color', '#'+window.agendasColors[agendaId]).css('color', 'black');
                if (!$(this).find('.calendar-location-name').length) {
                    $(this).append('<div class="calendar-location-name">' + locationName + '</div>');
                }
            });
        }
    }
}

function placeNotes(calendar) {

}

function erraRenderNotes(calendar) {
    var note_array = {};
    var i = 0;
    if ($('.wc-day-column-notes').length) {
        $('.wc-day-column-notes').remove();
    }
    if (!$('.wc-day-column-notes').length) {
        var jqXHRs = $('.wc-day-column-header').each(function() {
            // Get data from text 
            var date = strstr($(this).html(), '<br>').toString();
            date = date.replace('<br>', '');
            var date_time = fr_date_str_to_timestamp(date);
            note_array[i] = date_time;
            var noteDiv = '<div id="note-' + date_time + '" class="wc-day-column-notes pointer" data-date-time="' + date_time + '">Notes</div>';
            $(this).prepend(noteDiv);
            i++;

        });
        $.when.apply(null, jqXHRs.get()).done(
                function() {
                    $('.wc-day-column-notes').each(function() {
                        for (var dataTime in window.calendarCache['note']) {
                            if ($(this).attr('data-date-time').toString() == dataTime.toString()) {
                                $(this).attr('data-content', window.calendarCache['note'][dataTime]);
                                if (window.calendarCache['note'][dataTime].length) {
                                    $(this).append('<div class="note-reminder"></div>');
                                }
                            }
                        }
                    });
                }
        );
    }
    $('.wc-day-column-notes').off('click').on('click', function() {
        // Try to get note
        $noteDiv = $(this);
        var note_id = $noteDiv.attr('data-date-time');
        $('#handle-note textarea').val($noteDiv.attr('data-content'));
        alert_jqueryui($('#handle-note'), "Enregistrer", "Annuler", function() {
            var noteValue = $('#handle-note textarea').val();
            var updatedNote = {};
            $noteDiv.attr('data-content', noteValue);
            updatedNote['note_id'] = note_id;
            updatedNote['note_content'] = noteValue;
            updatedNote['access_id'] = $('.calendar-config-items').attr('data-access');
            addToLocalStore('SAVE_NOTE', updatedNote);
            $('#handle-note').dialog('destroy');
            // Parse data bcak in calendar
            for (var dataTime in window.calendarCache['note']) {
                if (note_id == dataTime.toString()) {
                    window.calendarCache['note'][note_id] = noteValue;
                }
            }
        });
    });
    load_help();
}

function erraRenderEvent(calEvent, $event) {
    // HEADER
    $event.find(".wc-time").css('font-size', '11px');
    $event.find(".wc-time").css('background', '#0084CC');
    if (calEvent.end.getTime() < new Date().getTime()) {
        $event.css("backgroundColor", "#" + calEvent.color);
        $event.find(".wc-time").css({
            "backgroundColor": "#999"
        });
    }
    // EVENT
    $event.css("background-color", "#" + calEvent.color);
    $event.css("color", "black");

    // Add close
    $event.find(".wc-time").attr('data-event-id', calEvent.id);
    $event.find(".wc-time").attr('data-occurency', calEvent.occurency);
    $event.find(".wc-time").attr('data-event-type', calEvent.event_type);


    // title
    if (calEvent.event_type == 'presta') {
        var eventName = calEvent.title;
        var eventHourString = timestamp_to_hour_str(calEvent.start) + ' - ' + timestamp_to_hour_str(calEvent.end);
        var eventDateString = timestamp_to_euro_date_hybrid(calEvent.start);
        var help = '<div class="calendar-hover">';
        help += '<strong>' + eventHourString + '</strong><br/>';
        help += 'Le ' + eventDateString + '<br/>';
        help += '<hr>';
        help += '<strong>' + eventName + '</strong><br/>';
        help += '<hr>';
        help += 'Client : ' + calEvent.nom_client + ' ' + calEvent.prenom_client + '<br/>';
        if (calEvent.telephone_client != undefined && calEvent.telephone_client.length) {
            help += 'Téléphone : ' + calEvent.telephone_client;
        }
        if (calEvent.dci != undefined && calEvent.dci.length) {
            help += '<hr/>Desc. interne : ' + calEvent.dci;
        }

        if (calEvent.paypal != undefined && calEvent.paypal == '1') {
            $event.css('border', '1px dashed green');
            $event.find(".wc-time").css('background', 'green');
            help += '<hr/><strong>Prestation reglée via PayPal</strong> <img src="' + IMAGES + '/pictos/logo_mini_paypal.jpg"/>';
        }
        help += '</div>';
        $event.addClass('live-tipsy').attr('title', help);

        if (calEvent.status_rdv == 'en_attente' && calEvent.end.getTime() < new Date().getTime() - (3600 * 24 * 1000)) {

            /*(function pulse(){
             $event.delay(200).fadeTo(200, 0.5).delay(50).fadeTo(200, 1,pulse);
             });*/
            $event.css('border', '1px dashed red');
            $event.find(".wc-time").css('background', 'red');
            if ($('.calendar-config-items').attr('data-show-notifications') == 1 && $('.calendar-config-items').attr('data-notification-past') == undefined) {
                $event.effect("pulsate", {
                    times: 2
                }, 1000);
                notification_jqueryui("N'oubliez pas de préciser le statut des rendez-vous passés (clients pas venus, etc...)");
                $('.calendar-config-items').attr('data-notification-past', 1);
            }
        }
    }
}

function erraPostRender() {
    // Add delete item
    if ($('#calendar').length) {
        $('.wc-cal-event .wc-time').each(function() {
            $(this).html($(this).html().replace('to', 'à'));
            var delete_event = '<div id="delete-event-' + $(this).attr('data-event-id') + '-' + $(this).attr('data-occurency') + '" class="calendar-event-close pointer"></div>';
            $(this).append(delete_event);
        });
    }
    click($('div[id^="delete-event-"]'), function($this) {
        $this.data('clicked', true);
    });
    $('.wc-cal-event').mouseenter(function() {
        $(this).find('.calendar-event-close').show();
    });
    $('.wc-cal-event').mouseleave(function() {
        $(this).find('.calendar-event-close').hide();
    });
    load_help();
}


function saveDroppedEvent(newCalEvent, post_array, exec) {
    var slot_day = parseInt(timestamp_to_day_time_php(new Date(newCalEvent.start).getTime()), 10);
    var slot_start = new Date(newCalEvent.start).getTime();
    var slot_end = new Date(newCalEvent.end).getTime();
    if (newCalEvent.occurency == 'original') {
        post_array['slot_day'] = parseInt(slot_day, 10);
    }
    post_array['slot_start'] = parseInt((slot_start / 1000) - slot_day, 10);
    post_array['slot_end'] = parseInt((slot_end / 1000) - slot_day, 10);
    return ajaxForce(WEBROOT + "/reattribute_event", post_array, function(data) {
        exec(data);
    });
}

function replaceHiddenId($editableDiv, newId) {
    var hiddenValue = $editableDiv.find('input[type="hidden"]').val();
    var beforeId = strstr(hiddenValue, '¤©id¤§', true) + '¤©id¤§';
    var afterId = strstr(hiddenValue, '¤©build_args');
    $editableDiv.find('input[type="hidden"]').val(beforeId + newId + afterId);
    $editableDiv.attr('id', 'editable_in_place_' + newId);
}